package com.example.service;


import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.dao.XueyuanInfoDao;
import com.example.entity.TeacherInfo;
import com.example.entity.XueyuanInfo;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.List;

@Service
public class XueyuanInfoService {
    @Resource
    private XueyuanInfoDao xueyuanInfoDao;


    public void add(XueyuanInfo xueyuanInfo) {
        XueyuanInfo info=xueyuanInfoDao.findByName(xueyuanInfo.getName());
        if(ObjectUtil.isEmpty(xueyuanInfo.getDecription())){
            throw new CustomException("-1","学分限制不能为空");
        }
        if(ObjectUtil.isNotEmpty(info)){
            throw  new CustomException(ResultCode.USER_EXIST_ERROR);
        }

        xueyuanInfoDao.insertSelective(xueyuanInfo);
    }

    public List<XueyuanInfo> findAll() {
        return xueyuanInfoDao.selectAllXueyuan();
    }

    public void update(XueyuanInfo xueyuanInfo) {
            xueyuanInfoDao.updateById(xueyuanInfo.getId(),xueyuanInfo.getName(),xueyuanInfo.getDecription(),xueyuanInfo.getScore());
    }

    public void deleteById(Long id) {
        xueyuanInfoDao.deleteById(id);

    }

    public PageInfo<XueyuanInfo> findPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<XueyuanInfo> infos = xueyuanInfoDao.selectAllTeacher();
        return PageInfo.of(infos);
    }

    public PageInfo<XueyuanInfo> findPageName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<XueyuanInfo> infos =xueyuanInfoDao.findByNamePage(name);
        return PageInfo.of(infos);
    }
}
